package com.baturu.vin.honda.script;

import com.baturu.vin.honda.dal.dao.HondaPartDAO;
import com.baturu.vin.honda.dal.dao.HondaTestDAO;
import com.baturu.vin.honda.dto.HondaNewCarTypeDTO;
import com.baturu.vin.honda.dto.HondaPartsInfoDTO;
import com.baturu.vin.honda.dto.HondaVinInfoDTO;
import com.baturu.vin.honda.service.impl.HondaEPCAnalyser;
import com.baturu.vin.test.AbstractBaseCase;
import com.baturu.vin.test.SimpleTestService;
import com.google.common.base.Splitter;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import static com.baturu.vin.test.excel.ExcelUtil.getChooserFile;

/**
 * @Author: chenjiahao
 * @Time: 2019/8/28
 */
public class NewCarType extends AbstractBaseCase {

    @Autowired
    HondaTestDAO hondaTestDAO;
    @Autowired
    HondaPartDAO hondaPartDAO;
    @Autowired
    HondaEPCAnalyser hondaEPCAnalyser;

    @Test
    public void testCarType() {
        File file = new File("C:\\Users\\ljy\\Desktop\\1.txt");
        try {
            PrintStream ps = new PrintStream(new FileOutputStream(file));

            SimpleTestService.FileChooserDTO chooserFile = getChooserFile();
            List<HondaNewCarTypeDTO> collect = chooserFile.getData().stream().map(d -> d.get(0)).filter(StringUtils::isNotBlank).flatMap(d -> {
                String vinPre11 = d.substring(0, 8) + "*" + d.substring(9, 11);
                String vinSub9 = d.substring(9);
                List<HondaNewCarTypeDTO> hondaNewCarTypeDTOS = hondaTestDAO.queryCarType(vinPre11, vinSub9).stream().peek(q -> q.setVinCode(d)).collect(Collectors.toList());
                if (hondaNewCarTypeDTOS.size() == 1) {
                    return hondaNewCarTypeDTOS.stream();
                }
                if (CollectionUtils.isEmpty(hondaNewCarTypeDTOS)) {
                    hondaNewCarTypeDTOS.add(HondaNewCarTypeDTO.builder().vinCode(d).build());
                    return hondaNewCarTypeDTOS.stream();
                }
                HondaVinInfoDTO hondaVinInfoDTO = hondaEPCAnalyser.analyseVinCode(d);
                if (hondaVinInfoDTO == null) {
                    hondaNewCarTypeDTOS.add(HondaNewCarTypeDTO.builder().vinCode(d).build());
                    return hondaNewCarTypeDTOS.stream();
                }
                List<HondaNewCarTypeDTO> collect1 = hondaNewCarTypeDTOS.stream().filter(q -> filterByCode(q.getPartsCode(), hondaVinInfoDTO)).collect(Collectors.toList());
                if ((collect1.size() > 1 && collect1.stream().allMatch(q -> StringUtils.isBlank(q.getPartsCode())))
                        || collect1.size() == 1
                        || (collect1.size() > 1 && collect1.stream().noneMatch(q -> q.getPartsCode().contains("/")))) {
                    return collect1.stream();
                }
                return collect1.stream().filter(q -> StringUtils.isNotBlank(q.getPartsCode())).filter(q -> !q.getPartsCode().contains("/"));
            }).collect(Collectors.toList());

            Map<String, Set<String>> collect1 = collect.stream().filter(q->!"null".equals(q.getThireId()))
                    .collect(Collectors.groupingBy(HondaNewCarTypeDTO::getVinCode,
                            Collectors.mapping(HondaNewCarTypeDTO::getThireId, Collectors.toSet())));
            long count = collect1.values().stream().filter(q -> q.size() == 1).count();
            collect.forEach(d -> ps.println(d.getBtrId() + "|" + d.getVinCode() + "|" + d.getThireId() + "|" + d.getPartsCode()));
            System.out.println((float) count / (float)collect.size());
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

    private boolean filterByCode(String partsCode, HondaVinInfoDTO hondaVinInfoDTO) {
        if (StringUtils.isBlank(partsCode) || partsCode.contains("国")) {
            return true;
        }
        List<String> partsCodes = Splitter.on("/").splitToList(partsCode);
        Set<HondaPartsInfoDTO> partsInfoByCodeAndVin = hondaPartDAO.findPartsInfoByCodeAndVin(partsCodes, hondaVinInfoDTO.getNpl(), hondaVinInfoDTO.getDisk());
        if (CollectionUtils.isEmpty(partsInfoByCodeAndVin)) {
            return false;
        }
        List<String> collect = partsInfoByCodeAndVin.stream().map(HondaPartsInfoDTO::getNpartgenu).collect(Collectors.toList());
        return collect.containsAll(partsCodes);
    }


}
